﻿using System;
using System.Collections.Generic;
using System.Text;
using WMS.IDAL;
using WMS.Entity;
using System.Data.SQLite;
using WMS.DAL.DBUtility;
using System.Data;

namespace WMS.DAL.SQLite
{
    public class UnitDB : IUnit
    {
        private readonly string insertSqlString = "insert into Unit([Name]) values(@Name)";
        private readonly string updateSqlString = "update Unit set [Name]=@UnitName where UnitID = @UnitID";
        private readonly string deleteSqlString = "delete from Unit where UnitID = {0}";
        private readonly string GetAllSqlString = "select UnitID,Name from unit";

        /// <summary>
        /// 返回全部计量单位
        /// </summary>
        /// <returns></returns>
        public IList<WMS.Entity.UnitEntity> GetAllUnit()
        {
            #region
            IList<UnitEntity> unitList = new List<UnitEntity>();

            using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(SQLiteHelper.ConnectionString, CommandType.Text, GetAllSqlString, null))
            {
                while (reader.Read())
                {
                    UnitEntity entity = new UnitEntity();
                    entity.UnitID = reader.GetInt32(0);
                    entity.UnitName = reader.GetString(1);

                    unitList.Add(entity);
                }

                if (reader.IsClosed == false)
                    reader.Close();
            }

            return unitList;
            #endregion
        }

        public WMS.Entity.UnitEntity GetUnitInfo(int unitID)
        {
            throw new NotImplementedException();
        }

        public int AddUnitInfo(WMS.Entity.UnitEntity entity)
        {
            #region
            try
            {
                SQLiteParameter[] para = new SQLiteParameter[]
                {
                    new SQLiteParameter("@Name",DbType.String)
                };
                para[0].Value = entity.UnitName;

                return SQLiteHelper.ExecuteNonQuery(SQLiteHelper.ConnectionString, CommandType.Text, insertSqlString, para);
            }
            catch
            {
                return 0;
            }

            #endregion
        }

        public int UpdateUnitInfo(WMS.Entity.UnitEntity entity)
        {
            #region
            try
            {
                SQLiteParameter[] para = new SQLiteParameter[]
                {
                    new SQLiteParameter("@UnitID",DbType.Int32),
                    new SQLiteParameter("@UnitName",DbType.String)
                
                };
                para[0].Value = entity.UnitID;
                para[1].Value = entity.UnitName;

                return SQLiteHelper.ExecuteNonQuery(SQLiteHelper.ConnectionString, CommandType.Text, updateSqlString, para);
            }
            catch
            {
                return 0;
            }
            #endregion
        }

        public int DeleteUnitInfo(WMS.Entity.UnitEntity entity)
        {
            return DeleteUnitInfo(entity.UnitID);
        }

        public int DeleteUnitInfo(int unitID)
        {
            #region
            try
            {
                return SQLiteHelper.ExecuteNonQuery(SQLiteHelper.ConnectionString, CommandType.Text, string.Format(deleteSqlString, unitID), null);
            }
            catch
            {
                return 0;
            }
            #endregion
        }
    }
}
